package com.uit.gis.util;

import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.ImageIcon;

public class Util {

	public static ImageIcon getApplicationIcon() {
		java.net.URL imgURL = Util.class.getClassLoader().getResource(
				"resources/app_icon.png");
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file");

		}
		return null;
	}

	public enum MessageBoxIcon {
		QUESTION, WARNNING, INFORMATION, ERROR,
	}

	public enum MessageBox {
		YESNOCANCEL, YESNO, ABORTRETRYIGNORE, OKCANCEL, OK,
	}

	public enum SpatialType {

		LINES("2006"), CUSTOMSHAPE("2006"), POINT("2005")/* Multipoint */, QUAD(
				"2003"), RECTANGLE("2003"), TRIANGLE("2003"), BOX("3003"), ELLIPSE(
				"2002"), ARCS("2002"), CIRCLE("2003");
		public String type;

		SpatialType(String type) {
			this.type = type;
		}

		public SpatialType getSpatialType(String type) {
			for (SpatialType b : SpatialType.values()) {
				if (type.equals(b.type))
					return b;
			}
			return null;
		}
	}

	public static final byte[] keyBytes = { (byte) 0x4e, (byte) 0xd5,
			(byte) 0xf8, (byte) 0x38, (byte) 0xb9, (byte) 0x34, (byte) 0x8b,
			(byte) 0x46, (byte) 0x5a, (byte) 0x05, (byte) 0xce, (byte) 0x7b,
			(byte) 0x8f, (byte) 0x39, (byte) 0xdc, (byte) 0x87 };

	public static byte[] aes_Encrypt(byte[] raw, byte[] clear) throws Exception {
		byte[] iv = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);
		byte[] encrypted = cipher.doFinal(clear);
		return encrypted;

	}

	public static byte[] aes_Decrypt(byte[] raw, byte[] encrypted)
			throws Exception {
		byte[] iv = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
		AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec, paramSpec);
		byte[] decrypted = cipher.doFinal(encrypted);
		return decrypted;
	}

	public static String getArrayElemnt(int[] array) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			result.append(array[i]);

			if (i < array.length - 1) {
				result.append(",");
			}
		}
		return new String(result); 
	}
	public static String getOrdinateElement(double[] array) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			result.append(array[i]);

			if (i < array.length - 1) {
				result.append(",");
			}
		}
		return new String(result);

	}
}
